
package com.zoo.transfer

import javax.servlet.ServletConfig
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import groovy.sql.Sql
import javax.naming.InitialContext

import com.mongodb.BasicDBObject
import com.mongodb.MongoClient
//import com.mongodb.ServerAddress

class HttpJobs extends HttpServlet {

    def conv, sql, db

    def void init(ServletConfig config) {
	super.init(config)

        conv = new Convert()

        def ds = new InitialContext().lookup("jdbc/mysql")
        sql = new Sql(ds)

        db = new MongoClient("192.168.1.200" , 30001).getDB("store")
    }

    def void processRequest(request, response) {

        def json

        switch(request.getParameter('do')) {
            case 'cust':
                switch(request.getParameter('ex')) {
                    case 'update':
                       def pageSize = 1
                       for(page in 0..1) {
                            if(page == 1) break // test for 1 item
                            custs = conv.customerTable(sql, db, page, pageSize)
                            if(!custs.size()) break
                       }

                        //json = mongo.updateCustomerTable(array)
                        break
                }
                break

            case 'ship':
                switch(request.getParameter('ex')) {
                }
                break

            default: json = '{success:false}'
        }

        response.setContentType("text/html;charset=UTF-8")
        response.getWriter().print(json)
    }


    def void doGet(HttpServletRequest request, HttpServletResponse response) {
        processRequest(request, response)
    }
    def void doPost(HttpServletRequest request, HttpServletResponse response) {
        processRequest(request, response)
    }
}

